package server;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.UUID;

/**
 * User: tum0r
 * Package Name: server
 * Time: 2019-03-12 Tuesday 15:38:40
 */
public class SubjectServer extends BaseServer {

    public JSONArray getAllSubjectInformation() {
        JSONArray result = new JSONArray();
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select * from subject order by title", null);
        LinkedHashMap<String, Object> item;
        Iterator<LinkedHashMap<String, Object>> iterator = temp.iterator();
        JSONObject items;
        Iterator<String> keys;
        String key;
        while (iterator.hasNext()) {
            item = iterator.next();
            items = new JSONObject();
            keys = item.keySet().iterator();
            while (keys.hasNext()) {
                key = keys.next();
                items.put(key, item.get(key) != null ? item.get(key) : "");
            }
            result.add(items);
        }
        return result;
    }

    public JSONObject getSubjectInformationByID(String subjectID) {
        JSONObject result = new JSONObject();
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select * from subject where uuid=?", new Object[]{subjectID});
        LinkedHashMap<String, Object> item;
        if (temp.size() > 0) {
            item = temp.get(0);
            Iterator<String> keys = item.keySet().iterator();
            String key;
            while (keys.hasNext()) {
                key = keys.next();
                result.put(key, item.get(key) != null ? item.get(key) : "");
            }
        }
        return result;
    }

    public void addSubject(String title, String content, String format, String testCase, String testCaseResult, long cppTimeLimit, long cppMemoryLimit, long javaTimeLimit, long javaMemoryLimit, long pythonTimeLimit, long pythonMemoryLimit) {
        JSONArray answer_limit = new JSONArray();

        JSONObject limit = new JSONObject();
        limit.put("language", "Java");
        limit.put("timeLimit", javaTimeLimit);
        limit.put("memoryLimit", javaMemoryLimit);
        answer_limit.add(limit);

        limit = new JSONObject();
        limit.put("language", "C/CPP");
        limit.put("timeLimit", cppTimeLimit);
        limit.put("memoryLimit", cppMemoryLimit);
        answer_limit.add(limit);

        limit = new JSONObject();
        limit.put("language", "Python");
        limit.put("timeLimit", pythonTimeLimit);
        limit.put("memoryLimit", pythonMemoryLimit);
        answer_limit.add(limit);

        sql.update("insert into subject values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{UUID.randomUUID().toString(), title, content, format, "", testCase, testCaseResult, answer_limit.toString(), "", 1});
    }

    public void changeSubject(String uuid, String title, String content, String format, String testCase,
                              String testCaseResult, long cppTimeLimit, long cppMemoryLimit, long javaTimeLimit,
                              long javaMemoryLimit,long pythonTimeLimit,long pythonMemoryLimit) {
        JSONArray answer_limit = new JSONArray();

        JSONObject limit = new JSONObject();
        limit.put("language", "Java");
        limit.put("timeLimit", javaTimeLimit);
        limit.put("memoryLimit", javaMemoryLimit);
        answer_limit.add(limit);

        limit = new JSONObject();
        limit.put("language", "C/CPP");
        limit.put("timeLimit", cppTimeLimit);
        limit.put("memoryLimit", cppMemoryLimit);
        answer_limit.add(limit);

        limit = new JSONObject();
        limit.put("language", "Python");
        limit.put("timeLimit", pythonTimeLimit);
        limit.put("memoryLimit", pythonMemoryLimit);
        answer_limit.add(limit);

        sql.update("update subject set title=?, content=?, format=?, test_case=?, test_case_result=?, answer_limit=? where uuid=?", new Object[]{title, content, format, testCase, testCaseResult, answer_limit.toString(), uuid});
    }

    public void deleteSubject(String uuid) {
        sql.update("delete from subject where uuid=?", new Object[]{uuid});
        sql.update("delete from record where subject_id=?", new Object[]{uuid});
    }

    public void clearSubject() {
        sql.update("delete from subject", null);
        sql.update("delete from record", null);
    }
}
